// pages/classification/classification.js
const app = getApp()
const share = require('../../components/utils.js')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.Imageurl,
    // 左侧菜单下标
    menuIndex:0,
    // 右侧滚动view
    rightViewId:'',
    curCategory :1,
    scrolly:true,
    scrollanimation:true,
    scrollHei:'500',
    isloadcomplete:true,
    goodsClass:[
      {
        id:1,
        name:'热热菜',
        goodsClass:[
          {
            img:'../../images/myimg10.jpg',
            cateName:'鱼香肉丝',
            cateDesc:'土豆+木耳+鸡蛋',
            currPrice:'9.9',
            yjPrice:'120',
            num:0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名萨达撒多撒多放松放松大放送大放送佛',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名2',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
        ]
      },
      {
        id: 2,
        name: '凉凉菜',
        list: [
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名3',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名4',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
          ,
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名5',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
        ]
      },
      {
        id: 3,
        name: '精品',
        list: [
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名6',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名7',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          },
          {
            img: '../../images/myimg10.jpg',
            cateName: '菜名8',
            cateDesc: '菜的描述',
            currPrice: '9.9',
            yjPrice: '120',
            num: 0
          }
        ]
      }
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGoodsClass()
  },
  //去搜索
gosearch(){
  wx.navigateTo({
    url: '/pages/search/search',
  })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight,
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: 1,
      show:true
    })
  }

  },

  // 点击左侧菜单栏
  changeMenu(e){
   
    var t=this
    console.log(e)
    let rightViewId = e.currentTarget.dataset.id,
        menuIndex = e.currentTarget.dataset.i
        console.log( menuIndex)
    this.data.goodsClass.forEach(element => {
      if(element.id==e.currentTarget.dataset.listid){
        t.setData({
          goodsLists:element.goodsList
        })
      }
    });
    this.setData({
      curCategory:menuIndex+1,
      rightViewId,
      menuIndex,
      activelist:e.currentTarget.dataset.listid
    })
    
    console.log(this.data.curCategory)
  },

// 获取右侧滚动信息
  getScroll(e){
    // console.log(e)
    let that = this,
        sTop = e.detail.scrollTop,
        categorys = that.data.goodsClass,
        sumHeight = 0//自定一个一个变量，用来存每个菜类和菜品一共的高度
        
     //chiak 建议，后期加个判断 到底部
    for (let index in categorys) {  
      console.log(categorys[index])            //循环所有菜类和菜品
      let item = categorys[index],      //菜类
          dishSize = item.goodsList.length;//每个菜类下的所有菜品
      //用定义的变量 加等于 每个菜类标题的高度（15）+ 每个菜类下的所有菜品的数量（dishSize ）乘以 每个菜品的高度（100）
      //就是右侧菜单所有菜类标题和菜品的一共高度
      //（这里的15 和100代表的是像素（px））
      sumHeight += 21 + dishSize * 70;  //这里的21和70最好写活，元素渲染完成后获取单个菜品的高度
      if (sTop <= sumHeight) {//判断当前滚动到顶部隐藏部分的高度，如果小于等于右侧菜单所有菜类和菜品的一共高度
        
        console.log(sTop,sumHeight)//那就把当前高度的 菜品属于所在类型的id传给左侧菜单的scroll-into-view 对应值里
        that.setData({
          curCategory: item.listid,
        })
        break;              //跳出当前循环
      }
    }
  },
  //获取商品分类
  getGoodsClass(){
    var t=this
    wx.request({
      url: app.globalData.Interfaceurl + 'getGoodsClass',
      method: "post",
      header: {
          "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
          userId: app.globalData.userId,
      },
      success: res => {
          
          res.data.goodsClass.forEach((element,index)=> {
            
            element.listid=index+1
             //console.log(element,index)
          });
          t.setData({

            goodsClass:res.data.goodsClass,
            activelist:res.data.goodsClass[0].id,
            goodsLists:res.data.goodsClass[0].goodsList,
            isloadcomplete:false
          })
          
          console.log(t.data.goodsClass)
          
      }
  })

  },
  //去商品详情页
  goProductdetails(e){
    // console.log(e)
    wx.navigateTo({
      url: '/pages/details/details?goodsid='+e.currentTarget.dataset.goodsid,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return share.share()

  }
})